package 二分查找;

import java.util.Arrays;
import java.util.List;

public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
//        int[] nums = new int[]{-1,0,3,5,9,12};
//        int target = 2;

        int[] nums = new int[]{-1,0,3,5,9,12};
        int length = nums.length;

        int target = 0;
        List<int[]> ints = Arrays.asList(nums);
        System.out.println(ints.indexOf(target));
    }

    public int search(int[] nums, int target,int start,int end){
        if(end -  start == 1){
            if(nums[start] == target){
                return start;
            }
            else if(nums[end] == target){
                return end;
            }
            else {
                return -1;
            }
        }

        int half = (end + start) / 2;
        if(nums[half] == target){
            return half;
        }
        else if(nums[half] < target){
            return search(nums, target,half,end);
        }
        else{
            return search(nums, target,start,half-1);
        }
    }

    public int search(int[] nums, int target) {
            int len = nums.length;

            if(len == 1){
                if(target == nums[0]){
                    return 0;
                }else {
                    return -1;
                }
            }

            int index =  search(nums,target,0,len - 1);

            return index;
    }
}
